//
//  SetPasswordViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/12.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class SetPasswordViewController: KMViewController {
    
    var phoneNumber = ""
    
    let passwordView = PWInputView()
    let confirmView = PWInputView()
    let changeBtn = UIButton()
    
    init(phoneNumber:String) {
        super.init(nibName: nil, bundle: nil)
        
        self.phoneNumber = phoneNumber
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = setPassword
        self.view.backgroundColor = UIColor.signBgColor()
        
        addSubView()
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        passwordView.text = password
        passwordView.placeholder = inputPassword
        passwordView.inputDelegate = self
        self.view.addSubview(passwordView)
        
        confirmView.text = confirmPassword
        confirmView.placeholder = inputPasswardAgain
        confirmView.inputDelegate = self
        self.view.addSubview(confirmView)
        
        changeBtn.backgroundColor = UIColor.lightGray
        changeBtn.isEnabled = false
        changeBtn.setTitle(change_title, for: .normal)
        changeBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        changeBtn.layer.cornerRadius = RATIO_H(maxNum: 20)
        changeBtn.layer.masksToBounds = true
        changeBtn.addTarget(self, action: #selector(changePasswordAction(sender:)), for: .touchUpInside)
        self.view.addSubview(changeBtn)
        
        passwordView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
        
        confirmView.snp.makeConstraints { (make) in
            make.top.equalTo(passwordView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
        
        changeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(confirmView.snp.bottom).offset(RATIO_H(maxNum: 30))
            make.left.equalTo(RATIO(maxNum: 20))
            make.right.equalTo(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
    }
    
    //MARK: 修改密码
    @objc fileprivate func changePasswordAction(sender:UIButton) {
        
        if passwordView.textView.text != confirmView.textView.text {
            
            KMTool.showText(_text: passwordNotSame)
            return
        }
        
        KMTool.waiting()
        KMTool.requestMethod(.post, URLString: url_setPassword, parameters: ["user_name":self.phoneNumber,"password":passwordView.textView.text ?? ""], success: { (value, json) in
                KMTool.stop()
            if json["status"].stringValue == "0" {
                KMTool.getUserNews(userName: self.phoneNumber, success: {
                       KMTool.setObject(self.phoneNumber, forEnumKey: .phone)
                    self.navigationController?.popToRootViewController(animated: true)
                })
            }
            
            KMTool.showText(_text: json["msg"].stringValue)
            
        }) {
                KMTool.stop()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}

extension SetPasswordViewController : PWInputViewDelegate {
    
    //MARK: 检查输入框内容
    func checkInputText() {
      
        if passwordView.textView.text != "" && confirmView.textView.text != "" {
            
            changeBtn.backgroundColor = UIColor.red
            changeBtn.isEnabled = true
        }else{
            changeBtn.backgroundColor = UIColor.lightGray
            changeBtn.isEnabled = false
        }
    }
    
}
